package br.ufrj.ppgi.jemf_advanced_example.contact;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Contact;
import br.ufrj.ppgi.jemf.mobile.view.ContactView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Contact detail screen.
 * This fragment is either contained in a {@link ContactListActivity} in two-pane mode (on tablets) or a {@link ContactDetailActivity} on handsets.
 */
public class ContactDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_CONTACT = "activated_contact";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "CONTACT";
    /**
     * The Contact content this fragment is showing.
     */
    private static Contact contact;
    /**
     * The Contact view holder.
     */
    private static ContactView contactView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public ContactDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Contact content specified by the fragment arguments.
        if (getArguments().containsKey(ContactDetailFragment.ARG_ID)) {
        	contact = getArguments().getParcelable(ContactDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Contact content
	    if (contact != null) {
	    	contactView = new ContactView(getActivity(), false);
	    	// Email
	    	contactView.setContactEmailText(contact.getEmail());
	    	// Phone
	    	contactView.setContactPhoneText(contact.getPhone());
	    	// Radio
	    	contactView.setContactRadioText(contact.getRadio());
	    	// Language
	    	contactView.setContactLanguageText(contact.getLanguageString());

	    	// Its necessary to add the ContactView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Contact View as a child of the ScrollView.
	    	scrollView.addView(contactView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Contact
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_CONTACT)) {
        	contact = savedInstanceState.getParcelable(STATE_CONTACT);
        }
	}





	/**
     * Get updated Contact
     * @return Contact
     */
	public static Contact getContact() {
		if (BuildConfig.DEBUG) Log.i(ContactListActivity.TAG, "ContactDetailFragment ::: getContact");
    	return contact;
    }

	/**
	 * Update Contact data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((contact != null) && (contactView != null)) {
    		if (BuildConfig.DEBUG) Log.i(ContactListActivity.TAG, "ContactDetailFragment ::: updateDataFromUI");
    		// Update Contact object with new data included by user through components
    		contact.setEmail(contactView.getContactEmailText());
    		contact.setPhone(contactView.getContactPhoneText());
    		contact.setRadio(contactView.getContactRadioText());
    		contact.setLanguageString(contactView.getContactLanguageText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Contact.
        outState.putParcelable(STATE_CONTACT, contact);
    }
}
